#!/bin/bash
#Describtion:
#This process deal with the items that need specific employee to verify when the gerrit commit -1
#
#Here we check :
#    1.obs_meta add/del/update
#    2.mkeuleros  add/del/update the rpmlist
#    3.sys-custom-tool add/del/update the rpmlist
#
#Author: Hongxun Ren
#Date: 2019-9-14

set -eu
source ../lib/repolib.sh

user="peulerosci"
repo_dir="/usr1/jenkins/EulerOS"
subpath=""

if [ ! -d /usr1/jenkins/EulerOS ]; then
    repo_create EulerOS_next EulerOS_next.xml
fi

GERRIT_BRANCH="${GERRIT_BRANCH}"
GERRIT_PROJECT="${GERRIT_PROJECT}"


function get_module_subpath()
{
    cd /usr1/jenkins/EulerOS_next/.repo/manifests
    git pull --no-tags &> /dev/null
    manifest="EulerOS_${GERRIT_BRANCH}.xml"
    if ! echo $manifest  | grep -E "*\.xml" &> /dev/null; then
        echo "[Error]: Not found the branch's manifest" >> "${error_log}"
        return 1
    fi
    
    temp_manifest1=""
    temp_manifest2=""
    temp_manifest3=""
    if cat $manifest | grep -v "<\!--" | grep "<include" &> /dev/null ;then
        temp_manifest1=$(cat $manifest | grep -v "<\!--" | grep "<include" | awk -F '"' '{print $2}')
        temp_manifest2=$(cat $temp_manifest1 | grep -v "<\!--" | grep "<include" | awk -F '"' '{print $2}')

        cat $temp_manifest1 | grep -v "<\!--" | grep "<include" | awk -F '"' '{print $2}' | while read line
        do
            if cat $line | grep -v "<\!--" | grep "<include" &> /dev/null ;then
                temp_manifest3=$(cat $line | grep -v "<\!--" | grep "<include" | awk -F '"' '{print $2}')
            fi
        done
    fi
    manifest="$manifest $temp_manifest1 $temp_manifest2 $temp_manifest3 EulerOS_docker.xml"
    cd - &> /dev/null

    if [ -d ${repo_dir}/.repo/manifests ];then
        cd ${repo_dir}/.repo/manifests
    else
        echo "[Error]: There is no manifest dir to find the patch of $GERRIT_PROJECT" >> "${error_log}"
        return 1
    fi
    
    count=0
    count=$(cat $manifest | grep "\"$GERRIT_PROJECT\"" | grep  -v "<\!--" | grep "<project" | wc -l)
    if [ $count -ne 0 ];then
        if [ $count -ne 1 ];then
            echo "[Warnning]: $GERRIT_PROJECT is ambiguous in manifest, we choice the first one ; please confirm it" >> "${error_log}"
            subpath=$(cat $manifest | grep -E "\"$GERRIT_PROJECT\"" | grep  -v "<\!--" | grep "<project" | awk -F '"' '{print $2}' | head -n 1)
        else
            subpath=$(cat $manifest | grep -E "\"$GERRIT_PROJECT\"" | grep  -v "<\!--" | grep "<project" | awk -F '"' '{print $2}')
        fi
    else
        echo "[Error]: $GERRIT_PROJECT has not been configed in manifest" >> "${error_log}"
        cd - &> /dev/null
        return 1
    fi
    cd - &> /dev/null
}

function get_update_files_list()
{
    prj_dir=$1
    cd "${prj_dir}"
    #patch_list=`git diff --name-status HEAD~1 HEAD~0 | grep -E "*\.patch$" | grep -E "^A|^R" |  awk '{print $2}'`
    ret=$(git diff --name-status HEAD~1 HEAD~0 | grep -v "COMMIT_MSG" | awk '{print $2}')
    cd - &> /dev/null
    echo "${ret}"
}

function get_modify_list()
{
    prj_dir=$1
    cd "${prj_dir}"
    ret=$(git diff --name-status HEAD~1 HEAD~0 | grep -v "COMMIT_MSG")
    cd - &> /dev/null
    echo "${ret}"
}

function fn_main()
{
    #init logfile
    local error_log=$(pwd)/error_log
    echo "********************Check Result: *******************" > "${error_log}"
    #get the source dir
    set +e
    get_module_subpath
    if [ $? -eq 1 ];then
        cat "${error_log}"
        exit 1
    fi
    set -e
    project_dir="${repo_dir}/${subpath}"

    ret=0
    #delete obs pkg -1
    if echo "${GERRIT_PROJECT}" | grep -E "obs_meta$" &> /dev/null  ;then
        modify_list=$(get_modify_list "${project_dir}")
        if echo "${modify_list}" | grep -E "^D.*projects/(.*/_service$|.*/prjconf$)" &> /dev/null;then
            #set verified -1
            echo "[Warning]: you are going to delete package or project, please verify this commit manually." >> "${error_log}"
            ret=1
        fi
    #remove module from list
    elif echo "${GERRIT_PROJECT}" | grep -E "mkeuleros$|sys-custom-tool$" &> /dev/null  ;then
        files_list=$(get_update_files_list "${project_dir}")
        if echo "${files_list}" | grep -E ".*/(.*/*\.lst$|.*/rpmlist$)" &> /dev/null;then
            cd "${project_dir}"
            for f in $(echo $files_list)
            do
                if git diff --word-diff HEAD~1 HEAD~0 "${f}"| grep -E "\[-.*-\]" &> /dev/null;then
                    echo "[Warning]: you are going to remove module from list[$f],please verify this commit manually." >> "${error_log}"
                    ret=1
                fi
            done 
            cd - &> /dev/null
        fi
    fi
    if [ "x$ret" == "x1" ];then
        cat "${error_log}"
        exit 1
    else
        echo "Success" >> "${error_log}"
        cat "${error_log}"
    fi
}

fn_main
